package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaomi.domain.Orders;
import com.xiaomi.domain.Phones;
import com.xiaomi.domain.User;
import com.xiaomi.service.OrdersService;
import com.xiaomi.service.PhonesService;
import com.xiaomi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author： YongZ
 * @date： 2024/6/4
 * @description：
 * @modifiedBy：
 */
@Controller
@Slf4j
public class ShopController {
    @Autowired
    private PhonesService phonesService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    /*
     * 空购物车*/
//    @RequestMapping("/shop_null")
//    public String shop_null() {
//        return "shop_null";
//    }

    /*
     * 购物车*/
//    @RequestMapping(value = "/shop_cart", method = RequestMethod.GET)
//    public String shop_cart(@RequestParam("username") String username, Model model) {
//        // 先判断传入的username是否在orders库中存在订单
//        Orders orders = ordersService.getOne(new LambdaQueryWrapper<Orders>().eq(Orders::getUsername, username));
//        if (orders != null) {
//            // 如果存在，则返回订单信息，否则返回空购物车
//            model.addAttribute("orders", orders);
//            return "shop_cart";
//
//        } else {
//            return "shop_null";
//        }
//    }

    @RequestMapping(value = "/shop_cart", method = RequestMethod.GET)
    public String shop_cart(@RequestParam("username") String username, Model model) {
        //Orders orders = ordersService.getOne(new LambdaQueryWrapper<Orders>().eq(Orders::getUsername, username));
        User existingUser = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username).last("limit 1"));
        if (username == null || username.equals("") || existingUser == null) {
            return "login";
        }
        List<Orders> orders = ordersService.list(new LambdaQueryWrapper<Orders>().eq(Orders::getUsername, username));
        log.info("orders: " + orders);
        System.out.println(orders);
        // 获取订单项并将其添加到Model
        model.addAttribute("ordersList", orders);
        model.addAttribute("username", username);
        return "cart";
    }

    @RequestMapping(value = "/shop/buy/detail", method = RequestMethod.GET)
//    public String detail(@RequestParam("id") Long id, @RequestParam(value = "username", required = false) String username, Model model) {
    public String detail(@RequestParam("product_id") Long product_id, @RequestParam(value = "username", required = false) String username, Model model) {
        User existingUser = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username).last("limit 1"));
        log.info("existingUser: " + existingUser);
        if (username == null || username.equals("") || existingUser == null) {
            return "login";
        }
        Phones phoneInfo = phonesService.getById(product_id);
        model.addAttribute("phoneInfo", phoneInfo);
        // 将phoneInfo.getVersion() json格式转换为List格式
        log.info("phoneInfo.getVersion(): " + phoneInfo.getVersion());
        JSONObject jsonObject = JSON.parseObject(phoneInfo.getVersion());
        Map<String, Object> VersionList = jsonObject.getInnerMap();
        log.info("VersionList: " + VersionList);
        model.addAttribute("versionList", VersionList);
        log.info("phoneInfo: " + phoneInfo);
        model.addAttribute("username", username);
        return "detail";
    }


}
